package com.boyinet.imesh2json.model

import com.boyinet.imesh2json.bean.DataMap
import com.boyinet.imesh2json.common.IdGenerator
import javafx.beans.property.LongProperty
import javafx.beans.property.SimpleLongProperty
import javafx.beans.property.SimpleStringProperty
import javafx.beans.property.StringProperty

class DataMapModel {

    val id: LongProperty = SimpleLongProperty(-1)
    val node: StringProperty = SimpleStringProperty("")
    val part: StringProperty = SimpleStringProperty("")
    val index: StringProperty = SimpleStringProperty("")
    val field: StringProperty = SimpleStringProperty("")
    private val templateId: LongProperty = SimpleLongProperty(-1)


    fun clear(tid: Long) {
        id.value = IdGenerator.instance.nextId()
        node.value = ""
        part.value = ""
        index.value = ""
        field.value = ""
        templateId.value = tid
    }

    fun fill(dataMap: DataMap) {
        id.value = dataMap.id
        node.value = dataMap.node.toString()
        part.value = dataMap.part.toString()
        index.value = dataMap.index.toString()
        field.value = dataMap.field
        templateId.value = dataMap.templateId
    }

    fun export(): DataMap {
        val current = this
        return DataMap(current.id.value).apply {
            node = current.node.value.toInt()
            part = current.part.value.toInt()
            index = current.index.value.toInt()
            field = current.field.value
            templateId = current.templateId.value
        }
    }
}